﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Petshop.Backend.Data
{
    class ProductRepository : Petshop.Backend.Data.IProductRepository, IDisposable
    {
        private readonly IDatabaseSession _databaseSession;
        public ProductRepository(IDatabaseSession databaseSession)
        {
            _databaseSession = databaseSession;
        }
        public IEnumerable<Product> RetrieveProductByCategeoryId(string categoryId)
        {
            Category category = _databaseSession.Context.Categories.SingleOrDefault(cat => cat.CatId == categoryId);
            return category.Products;
        }
        public IEnumerable<Product> FindProductBy(string text)
        {
            return _databaseSession.Context.Products.Where(c => c.Name.ToUpper().StartsWith(text.ToUpper())  | c.Category.ToUpper().StartsWith(text.ToUpper()));
        }
        #region IDisposable
        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _databaseSession.Context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
